home *** CD-ROM | disk | FTP | other *** search
Text File | 1988-05-15 | 2.1 KB | 63 lines | [TEXT/CCL ] |
- ; Ted Kaehler and Dave Patterson a taste of SmallTalk
- ; W. W. Norton ed., chapter 6, pp. 83 ff.
- ; translated in Allegro Common Lisp by Jean-Pascal J. LANGE.
- ; © Copyright 1988 Jean-Pascal J. LANGE.
-
- (proclaim '(optimize (speed 3)
- (space 0)
- (safety 0)
- (compilation-speed 0) ))
-
- (defStruct (HanoiDiskRules (:include HanoiDisk))
- ; previousPole number of the pole this disk was on previously.
- (previousPole nil) )
-
- ; access
-
- (deFun width (thisDisk)
- ; return the size of this disk
- (HanoiDiskRules-width thisDisk) ) ; width
-
- (deFun widthPoleRules (thisDisk size whichPole)
- ; invoke widthPole for HanoiDisk structure
- (widthPole thisDisk size whichPole)
- (setf (HanoiDiskRules-previousPole thisDisk) 1) ) ; widthPoleRules
-
- ; moving
-
- (deFun bestMove (thisDisk)
- ; If self can move two places, which is the best? Return the top
- ; disk of the pole that this disk has not been on recently.
- (let ((secondBest))
- (cond
- ((polesOtherThan
- *TheTowers*
- thisDisk
- #'(lambda (targetDisk)
- (cond ((< (width thisDisk)
- (width targetDisk) )
- (setq thisDisk targetDisk)
- (if (not
- (= (pole targetDisk)
- (HanoiDiskRules-previousPole thisDisk) ) )
- targetDisk ) )) ) ) )
- ; as a last resort, return a pole it was on recently
- (t secondBest) ) ) ) ; bestMove
-
- (deFun hasLegalMove (thisDisk)
- ; do either of the other two poles have a top disk large enough
- ; for this disk to rest on?
- (polesOtherThan *TheTowers*
- thisDisk
- ; when a pole has no disk,
- ; targetDisk is a mock disk with infinite width
- #'(lambda (targetDisk)
- (< (width thisDisk)
- (width targetDisk) ) ) ) ) ; hasLegalMove
-
- (deFun moveUponRules (thisDisk destination)
- ; this disk just moved. Record the new pole and tell the user.
- (setf (HanoiDiskRules-previousPole thisDisk) (pole thisDisk))
- ; run the version of moveUpon defined for structure HanoiDisk
- (moveUpon destination) ) ; moveUponRules
-